#include <cassert>

#include "GraphicsTool/DatasPublic.h"

#include <iostream>
#include <vector>



COctree::COctree(void)
{
}

COctree::COctree(COctree * parent) : mHalfSize(0, 0, 0)
{
	//initialize all children to null.
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			for (int k = 0; k < 2; k++)
			{
				mChildren[i][j][k] = 0;
			}
		}
	}

	mParent = parent;
	mNumNodes = 0;
}

void COctree::_addNode(COctreeNode * n)
{
	mNodes.push_back(n);
	n->setOctant(this);
	_ref();
}

void COctree::_removeNode(COctreeNode * n)
{
	mNodes.erase(std::find(mNodes.begin(), mNodes.end(), n));
	n->setOctant(0);
	////update total counts.
	//_unref();
}

void COctree::_getCullBounds(osg::BoundingBoxd *b) const
{
	b->set(mBox._min - mHalfSize, mBox._max + mHalfSize);
}

void COctree::_getChildIndexes(const osg::BoundingBoxd &box, int *x, int *y, int *z) const
{
	osg::Vec3f center = mBox.center();
	osg::Vec3f ncenter = box.center();

	if (ncenter[0] > center[0])
		* x = 1;
	else
		*x = 0;

	if (ncenter[1] > center[1])
		* y = 1;
	else
		*y = 0;

	if (ncenter[2] > center[2])
		* z = 1;
	else
		*z = 0;

}

bool COctree::_isTwiceSize( osg::BoundingBoxd &box) 
{
	//if (box.isInfinite())
	//	return false;

	osg::Vec3f halfMBoxSize = mBox.getHalfSize();
	osg::Vec3f boxSize = box.getSize();
	return ((boxSize[0] <= halfMBoxSize[0]) && (boxSize[1] <= halfMBoxSize[1]) && (boxSize[2] <= halfMBoxSize[2]));
}

COctree::~COctree(void)
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			for (int k = 0; k < 2; k++)
			{
				if (mChildren[i][j][k] != 0)
					delete mChildren[i][j][k];
			}
		}
	}

	mParent = 0;
}

